﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCCommon.OAEnums;
using BCDto.Common;
using BCDto.OA.TeamWorks.TaskRecord;
using BCService.OA.TeamWorks.TaskRecordService;
using BlackCoffeeRESTful.Configs;
using BCService;

namespace BlackCoffeeRESTful.Controllers.Management.OA.Workflow
{
    /// <summary>
    /// 工作流任务接口。
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class TasksController : ManagementBaseController
    {
        private readonly ITaskRecordService taskRecordService;

        public TasksController(ITaskRecordService taskRecordService)
        {
            this.taskRecordService = taskRecordService;
        }

        /// <summary>
        /// 获取带有分页的工作流任务。
        /// </summary>
        /// <param name="taskRecordNo">任务编号。</param>
        /// <param name="companyId">公司编号。</param>
        /// <param name="adminId">管理员编号。</param>
        /// <param name="workflowKeys">以逗号分割的工作流名称。</param>
        /// <param name="workflowActivityInstanceId">工作流活动实例编号。</param>
        /// <param name="referenceNumber">参考号。</param>
        /// <param name="isMyApproved">是否为需要我审批的。</param>
        /// <param name="startDate">起始日期。</param>
        /// <param name="endDate">结束日期。</param>
        /// <param name="status">任务状态。</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">使用正序排列吗?</param>
        /// <param name="pageIndex">以 1 开始的页码。</param>
        /// <param name="pageSize">每页显示的行数。</param>
        /// <returns>带有分页信息的工作流任务列表。</returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<TaskRecordDto>), 200)]
        public IActionResult GetTasks([FromQuery(Name = "task_record_no")] string taskRecordNo,
            [FromQuery(Name = "company_id")] int? companyId,
            [FromQuery(Name = "admin_id")] long? adminId,
            [FromQuery(Name = "workflow_keys")] string workflowKeys,
            [FromQuery(Name = "workflow_activity_instance_id")] long? workflowActivityInstanceId,
            [FromQuery(Name = "reference_number")] string referenceNumber,
            [FromQuery(Name = "is_my_approved")] bool? isMyApproved,
            [FromQuery(Name = "start_date")] DateTime? startDate,
            [FromQuery(Name = "end_date")] DateTime? endDate,
            [FromQuery(Name = "status")] OATaskHandleStatus? status,
            [FromQuery(Name = "sort_name")] string sortName,
            [FromQuery(Name = "ascending")] bool? ascending,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            var pageableList = this.taskRecordService.GetPageableList(taskRecordNo, companyId, adminId, workflowKeys.AsArrayArgument<string>(), workflowActivityInstanceId, referenceNumber, isMyApproved, startDate, endDate, status, sortName, ascending, pageIndex, pageSize);

            return this.Ok(pageableList);
        }

        /// <summary>
        /// 获取指定的工作流任务。
        /// </summary>
        /// <param name="taskRecordNo">工作流任务编号。</param>
        /// <response code="200">工作流任务详情。</response>
        /// <response code="204">指定的工作流任务不存在或已被删除。</response>
        /// <returns>工作流任务详情。</returns>
        [HttpGet("{task_record_no}")]
        [ProducesResponseType(typeof(TaskRecordViewDto), 200)]
        public IActionResult GetTask([FromRoute(Name = "task_record_no")] string taskRecordNo)
        {
            var taskRecord = this.taskRecordService.GetTask(taskRecordNo);

            return this.Ok(taskRecord);
        }

        /// <summary>
        /// 按照原业务编号获取相关的工作流任务列表。
        /// </summary>
        /// <param name="referenceNumber">原业务编号。</param>
        /// <response code="200">与指定业务相关的工作流任务列表。</response>
        /// <returns>与指定业务相关的工作流任务列表。</returns>
        [HttpGet("Reference/{reference_number}")]
        [ProducesResponseType(typeof(IEnumerable<TaskRecordViewDto>), 200)]
        public IActionResult GetTasksByReferenceNumber([FromRoute(Name = "reference_number")] string referenceNumber)
        {
            var taskRecords = this.taskRecordService.GetTasksByReferenceNumber(referenceNumber);

            return this.Ok(taskRecords);
        }

        /// <summary>
        /// 获取工作流任务原始引用对象。
        /// </summary>
        /// <param name="taskRecordNo">任务编号。</param>
        /// <returns></returns>
        [HttpGet("{task_record_no}/ReferenceObject")]
        [ProducesResponseType(typeof(object), 200)]
        public IActionResult GetReferenceObject([FromRoute(Name = "task_record_no")] string taskRecordNo)
        {
            throw new NotImplementedException();
        }

        ///// <summary>
        ///// 通过工作流任务。
        ///// </summary>
        ///// <param name="taskRecordNo">工作流任务编号。</param>
        ///// <param name="requestDto">备注和其他参数。</param>
        ///// <response code="200">已处理的工作流任务。</response>
        ///// <response code="204">指定的工作流任务不存在或已被删除。</response>
        ///// <returns>已处理的工作流任务。</returns>
        //[HttpPut("{task_record_no}/[action]")]
        //[Log("通过工作流任务")]
        //[ProducesResponseType(typeof(TaskRecordViewDto), 200)]
        //public IActionResult Approve([FromRoute(Name = "task_record_no")] string taskRecordNo, [FromBody] GeneralRequestDto requestDto)
        //{
        //    var taskRecord = this.taskRecordService.Approve(taskRecordNo, requestDto);

        //    return this.Ok(taskRecord);
        //}

        ///// <summary>
        ///// 驳回工作流任务。
        ///// </summary>
        ///// <param name="taskRecordNo">工作流任务编号。</param>
        ///// <param name="requestDto">备注和其他参数。</param>
        ///// <response code="200">已处理的工作流任务。</response>
        ///// <response code="204">指定的工作流任务不存在或已被删除。</response>
        ///// <returns>已处理的工作流任务。</returns>
        //[HttpPut("{task_record_no}/[action]")]
        //[Log("驳回工作流任务")]
        //[ProducesResponseType(typeof(TaskRecordViewDto), 200)]
        //public IActionResult Reject([FromRoute(Name = "task_record_no")] string taskRecordNo, [FromBody] GeneralRequestDto requestDto)
        //{
        //    var taskRecord = this.taskRecordService.Reject(taskRecordNo, requestDto);

        //    return this.Ok(taskRecord);
        //}

        ///// <summary>
        ///// 暂停工作流任务。
        ///// </summary>
        ///// <param name="taskRecordNo">工作流任务编号。</param>
        ///// <param name="requestDto">备注和其他参数。</param>
        ///// <response code="200">已处理的工作流任务。</response>
        ///// <response code="204">指定的工作流任务不存在或已被删除。</response>
        ///// <returns>已处理的工作流任务。</returns>
        //[HttpPut("{task_record_no}/[action]")]
        //[Log("暂停工作流任务")]
        //[ProducesResponseType(typeof(TaskRecordViewDto), 200)]
        //public IActionResult Pause([FromRoute(Name = "task_record_no")] string taskRecordNo, [FromBody] GeneralRequestDto requestDto)
        //{
        //    var taskRecord = this.taskRecordService.Pause(taskRecordNo, requestDto);

        //    return this.Ok(taskRecord);
        //}

        ///// <summary>
        ///// 关闭工作流任务。
        ///// </summary>
        ///// <param name="taskRecordNo">工作流任务编号。</param>
        ///// <param name="requestDto">备注和其他参数。</param>
        ///// <response code="200">已处理的工作流任务。</response>
        ///// <response code="204">指定的工作流任务不存在或已被删除。</response>
        ///// <returns>已处理的工作流任务。</returns>
        //[HttpPut("{task_record_no}/[action]")]
        //[Log("关闭工作流任务")]
        //[ProducesResponseType(typeof(TaskRecordViewDto), 200)]
        //public IActionResult Close([FromRoute(Name = "task_record_no")] string taskRecordNo, [FromBody] GeneralRequestDto requestDto)
        //{
        //    var taskRecord = this.taskRecordService.Close(taskRecordNo, requestDto);

        //    return this.Ok(taskRecord);
        //}

        /// <summary>
        /// 获取指定的工作流任务的回复记录。
        /// </summary>
        /// <param name="taskRecordNo">工作流任务编号。</param>
        /// <returns></returns>
        [HttpGet("{task_record_no}/Replies")]
        [ProducesResponseType(typeof(IEnumerable<TaskReplyRecordDto>), 200)]
        public IActionResult GetReplies([FromRoute(Name = "task_record_no")] string taskRecordNo)
        {
            var taskReplies = this.taskRecordService.GetTaskReplies(taskRecordNo);

            return this.Ok(taskReplies);
        }

        /// <summary>
        /// 为指定的工作流任务添加回复。
        /// </summary>
        /// <param name="taskRecordNo">工作流任务编号。</param>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        [HttpPost("{task_record_no}/Replies")]
        [Log("为指定的工作流任务添加回复")]
        [ProducesResponseType(typeof(TaskReplyRecordDto), 201)]
        public IActionResult CreateReply([FromRoute(Name = "task_record_no")] string taskRecordNo, [FromBody] TaskReplyRecordRequestDto requestDto)
        {
            var taskReply = this.taskRecordService.CreateTaskReply(taskRecordNo, requestDto);

            return this.Created("forbidden", taskReply);
        }
    }
}
